﻿#include "LPHeaderControl.h"
#include "ui_LPHeaderControl.h"

#include <QDir>
#include <QFile>
#include <QTimer>
#include <QPainter>
#include <QPointer>
#include <QMouseEvent>
#include <QStyleOption>
#include <QDateTime>
#include <QDesktopWidget>

#include "Controls/LPScrollArea/LPScrollArea.h"
struct LPHeaderControlPrivate
{
    LPHeaderControlPrivate()
    {

    }
    QPoint m_dragPosition;//拖拽事件鼠标坐标
    int m_nCurrentScreen;  //应用主窗体当前使用的屏幕序号
    QWidget * m_parent;
    typedef QList<LPMenuPage*> MenuPageArray;
    MenuPageArray m_menuPageList; //存储每一页数据结构
    QTimer * m_timer;
};

static int CurrentPage = 0;

LPHeaderControl::LPHeaderControl(QWidget *parent) :
    QWidget(parent),_p( new LPHeaderControlPrivate),
    ui(new Ui::LPHeaderControl)
{
    ui->setupUi(this);
    _p->m_parent = parent;
    installEventFilter(this);
    InitStyle();
    InitTimer();
}

LPHeaderControl::~LPHeaderControl()
{
    delete ui;
}

void LPHeaderControl::InitStyle()
{
    QString strDir = QDir::currentPath();
    QFile file( strDir + "/Theme/qss/TitileBar.qss" );
    if( file.open(QFile::ReadOnly) )
    {
        QString strStyleSheet = file.readAll();
        setStyleSheet( strStyleSheet );
        file.close();
    }
}

void LPHeaderControl::InitTimer()
{
    _p->m_timer = new QTimer();
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
    connect(_p->m_timer,SIGNAL(timeout()),this,SLOT(onm_timer_slots()));
#else
    connect(_p->m_timer,&QTimer::timeout,[=]()
    {
        ui->label_TextLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh:mm:ss"));
    });
#endif
    _p->m_timer->start(1000);
}

void LPHeaderControl::SetMenuName(const QString &Name)
{
    ui->label_MenuName->setText( Name );
}

void LPHeaderControl::SetAppName(const QString &Name)
{
    ui->label_TitleName->setText( Name );
}

LPMenuPage *LPHeaderControl::AddMenuGroup(const QString &Name)
{
    LPMenuPage * page = NULL;

    if( Name.isEmpty() )
    {
        return  page;
    }

    if( page = FindMenuPage( Name) )
    {
        return page;
    }
    else
    {
        page = new LPMenuPage;
        LPScrollArea * pageScrollArea = new LPScrollArea( ui->stackedWidget );
        pageScrollArea->setProperty("name", Name);

        page->_PageWidget = new QWidget( ui->stackedWidget );

        QSpacerItem * spacerItem = new QSpacerItem( 10, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
        QHBoxLayout * layout = new QHBoxLayout( page->_PageWidget );

        page->_Layout = new QGridLayout( );
        layout->addLayout(page->_Layout);
        page->_Layout->setSpacing(4);
        layout->addSpacerItem(spacerItem);

        layout->setContentsMargins(0,0,0,0);
        page->_PageWidget->setContentsMargins(0,0,0,0);
        page->_PageWidget->setLayout(layout);

        pageScrollArea->setWidget(page->_PageWidget);
        ui->stackedWidget->insertWidget( CurrentPage, pageScrollArea );

        page->_Name = Name;
        page->_CurrentIndex = CurrentPage;
        _p->m_menuPageList.push_back( page );
        CurrentPage++;

        SetStackedWidgetPage( 0 );
        return page;
    }
    return page;
}

bool LPHeaderControl::DeleteMenuGroup(const QString &Name)
{
    LPMenuPage * page = NULL;
    if( page = FindMenuPage(Name) )
    {
        QWidget * widget = ui->stackedWidget->widget( page->_CurrentIndex );
        ui->stackedWidget->removeWidget( widget );
        DeleteMenuGroup( Name );
        delete widget;
        return true;
    }
    return page;
}

QToolButton *LPHeaderControl::AddAction(LPMenuPage *Page, const int Row, const int Colnum, const QString ActionName)
{
    QToolButton * button = nullptr;
    if( !Page )
    {
        return button;
    }

    button = new QToolButton();
    button->setText( ActionName );
    button->setIconSize( QSize(30,30) );
    Page->_Layout->addWidget( button, Row, Colnum );
    button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
    Page->_Row = Row;
    Page->_Colnum = Colnum;
    return button;
}

LPMenuPage *LPHeaderControl::FindMenuPage(const QString &PageName)
{
    LPMenuPage * menuPage = NULL;
    for ( int i = 0; i<_p->m_menuPageList.size(); i++)
    {
        if( _p->m_menuPageList.at(i)->_Name == PageName )
        {
            menuPage = _p->m_menuPageList.at( i );
            return  menuPage;
        }
    }
    return  menuPage;
}

bool LPHeaderControl::DeleteMenuPage(const QString PageName)
{
    QList<LPMenuPage*>::iterator itor;
    for ( itor = _p->m_menuPageList.begin(); itor!= _p->m_menuPageList.end(); itor++)
    {
        if( (*itor)->_Name == PageName )
        {
            itor = _p->m_menuPageList.erase( itor );
            return true;
        }
    }
    return false;
}

void LPHeaderControl::AddGroupWidget(LPMenuPage *Page, const int Row, const int Colnum, QWidget *Widget)
{
    Page->_Layout->addWidget( Widget, Row, Colnum );
    Page->_Row = Row;
    Page->_Colnum = Colnum;
}

void LPHeaderControl::SetStackedWidgetPage(const int Index)
{
    ui->stackedWidget->setCurrentIndex( Index );
    if( QWidget * widget = ui->stackedWidget->currentWidget() )
    {
        QString name = widget->property("name").toString();
        SetMenuName( name );
    }
}

bool LPHeaderControl::eventFilter(QObject *obj, QEvent *event)
{
   if( obj == _p->m_parent)
   {
     if( event->type() == QEvent::Resize || event->type() == QEvent::Move )
     {
         AdjustWidgetSize();
     }
     else if( event->type() == QEvent::Show )
     {
         show();
     }
     else if(event->type() == QEvent::WindowStateChange)
     {
         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
         if(keyEvent->key() == Qt::WindowMaximized)
         {
             AdjustWidgetSize();
         }
     }
     else if( event->type() == QEvent::Hide )
     {
         hide();
     }
   }
   else
   {
       if( event->type() == QEvent::MouseButtonPress )
       {
           QMouseEvent * mouseEvent = static_cast< QMouseEvent*>(event);
            _p->m_dragPosition = mouseEvent->globalPos() - frameGeometry().topLeft();
       }
       else if( event->type() == QEvent::MouseButtonDblClick)
       {
           showMaximized();
       }
   }
   return QWidget::eventFilter( obj, event );
}

void LPHeaderControl::AdjustWidgetSize()
{
    if( _p->m_parent )
    {
        int width = QApplication::desktop()->geometry().width();
        int heigth = QApplication::desktop()->geometry().height();
        this->setGeometry( 0, 0, width, heigth/8);
    }
}

void LPHeaderControl::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if( event->button() == Qt::LeftButton )
    {
        _p->m_dragPosition = event->globalPos() - frameGeometry().topLeft();
    }
}

void LPHeaderControl::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    _p->m_parent->move( event->globalPos() - _p->m_dragPosition );
}

void LPHeaderControl::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QStyleOption opt;
    opt.init( this );
    QPainter painter(this);
    style()->drawPrimitive( QStyle::PE_Widget, &opt, &painter, this);
}

void LPHeaderControl::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    emit heightChange();
}

void LPHeaderControl::on_pbt_close_clicked()
{
    qApp->closeAllWindows();
    qApp->exit(0);
}

void LPHeaderControl::on_pbt_min_clicked()
{
    _p->m_parent->showMinimized();
}

void LPHeaderControl::on_pbt_Top_clicked()
{
    int currentCount = ui->stackedWidget->currentIndex();
    if( currentCount > 0 )
    {
        int index = currentCount - 1;
        ui->stackedWidget->setCurrentIndex( index );

        if( QWidget* pWidget = ui->stackedWidget->currentWidget() )
        {
            QString strName = pWidget->property("name").toString();
            SetMenuName( strName );
        }
    }
}

void LPHeaderControl::on_pbt_Bottom_clicked()
{
    int currentCount = ui->stackedWidget->currentIndex();
    if( currentCount < currentCount - 1 )
    {
        int index = currentCount + 1;
        ui->stackedWidget->setCurrentIndex( index );

        if( QWidget* pWidget = ui->stackedWidget->currentWidget() )
        {
            QString strName = pWidget->property("name").toString();
            SetMenuName( strName );
        }
    }
}

void LPHeaderControl::on_Timer_slots()
{
    ui->label_TextLabel->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd-hh:mm:ss"));
}
